﻿// 功能：MzGraphicsAPI的封装类
// 描述：
// 作者：温伟鹏
// 日期：2010/5/13 14:46:43

using System;
using System.Drawing;
using System.Runtime.InteropServices;

namespace MeizuSDK.Drawing
{
    /// <summary>
    /// MzGraphicsAPI的封装类
    /// </summary>
    public sealed class MzGraphicsAPI
    {
        /// <summary>
        /// 在指定的设备上下文中利用两个图像做指定的动画效果
        /// </summary>
        /// <param name="hBmp1">图像1</param>
        /// <param name="hBmp2">图像2</param>
        /// <param name="hdcDst">目的DC</param>
        /// <param name="prcDst">动画所在矩形</param>
        /// <param name="animType">动画类型</param>
        /// <param name="nSpeedH">横向速度</param>
        /// <param name="nSpeedV">纵向速度</param>
        [DllImport("MZFCWrapper.dll", EntryPoint = "AnimateBitmapEx", SetLastError = true)]
        private static extern void AninateBitmap(IntPtr hBmp1, IntPtr hBmp2, IntPtr hdcDst, ref RECT prcDst, uint animType, int nSpeedH, int nSpeedV);
        /// <summary>
        /// 在指定的设备上下文中利用两个图像做指定的动画效果
        /// </summary>
        /// <param name="img1">图像1</param>
        /// <param name="img2">图像2</param>
        /// <param name="hdc">目的DC</param>
        /// <param name="rect">动画所在矩形</param>
        /// <param name="animType">动画类型</param>
        /// <param name="speedH">横向速度</param>
        /// <param name="speedV">纵向速度</param>
        public static bool AnimateBitmap(Bitmap img1, Bitmap img2, IntPtr hdc, Rectangle rect, AnimationType animType, int speedH, int speedV)
        {
            try
            {
                RECT rect1 = RECT.FromRectangle(rect);

                AninateBitmap(img1.GetHbitmap(), img2.GetHbitmap(), hdc, ref rect1, (uint)animType, speedH, speedV);

                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 卸载 mzres.dll 
        /// </summary>
        [DllImport("MZFCWrapper.dll", EntryPoint = "FreeMzResModuleEx", SetLastError = true)]
        public static extern void FreeMzResModule();

        /// <summary>
        /// 卸载 MzResV2.dll
        /// </summary>
        [DllImport("MZFCWrapper.dll", EntryPoint = "FreeMzResV2ModuleEx", SetLastError = true)]
        public static extern void FreeMzResV2Module();

        /// <summary>
        /// 加载并返回 mzres.dll 的模块句柄 
        /// </summary>
        [DllImport("MZFCWrapper.dll", EntryPoint = "GetMzResModuleHandleEx", SetLastError = true)]
        public static extern IntPtr GetMzResModuleHandle();

        /// <summary>
        /// 加载并返回 MzResV2.dll 的模块句柄
        /// </summary>
        [DllImport("MZFCWrapper.dll", EntryPoint = "GetMzResV2ModuleHandleEx", SetLastError = true)]
        public static extern IntPtr GetMzResV2ModuleHandle();

        /// <summary>
        /// 获得指定位图的某一部分
        /// </summary>
        /// <param name="hBmpSrc">源位图</param>
        /// <param name="prcDst">要获取位图的某一部分所在的矩形</param>
        /// <returns>返回HBITMAP指针</returns>
        [DllImport("MZFCWrapper.dll", SetLastError = true, EntryPoint = "GetPartofBitmapEx")]
        private extern static IntPtr GetPartofBitmap(IntPtr hBmpSrc, ref RECT prcDst);
        /// <summary>
        /// 获得指定位图的某一部分
        /// </summary>
        /// <param name="bmpSrc">源位图</param>
        /// <param name="prcDst">要获取位图的某一部分所在的矩形</param>
        /// <returns></returns>
        public static Bitmap GetPartOfBitmap(Bitmap imgSrc, Rectangle prcDst)
        {
            if (imgSrc == null)
                return null;

            IntPtr hBmpSrc = imgSrc.GetHbitmap();

            if (hBmpSrc == IntPtr.Zero)
                return null;

            RECT rect = RECT.FromRectangle(prcDst);
            IntPtr hBitmap = GetPartofBitmap(hBmpSrc, ref rect);

            return Bitmap.FromHbitmap(hBitmap);
        }

        /// <summary>
        /// 创建32位带Alpha通道的兼容位图 
        /// </summary>
        /// <param name="width">位图宽度</param>
        /// <param name="height">位图高度</param>
        /// <returns>返回HBITMAP指针</returns>
        [DllImport("MZFCWrapper.dll", SetLastError = true, EntryPoint = "MzCreateCompatibleAlphaBitmapEx")]
        public extern static IntPtr CreateCompatibleAlphaBitmap(int width, int height);

        /// <summary>
        /// 绘制系统定义的控件外观 
        /// </summary>
        /// <param name="hdcDst">目的DC</param>
        /// <param name="prcDst">目的矩形</param>
        /// <param name="nControlType">控件类型(参见 MzControlDefs.h 中的定义)</param>
        /// <param name="nControlState">控件的状态(参见 MzControlDefs.h 中的定义)</param>
        /// <returns></returns>
        [DllImport("MZFCWrapper.dll", SetLastError = true, EntryPoint = "MzDrawControlEx")]
        private extern static bool DrawControl(IntPtr hdcDst, ref RECT prcDst, uint nControlType, uint nControlState);
        /// <summary>
        /// 绘制系统定义的控件外观 
        /// </summary>
        /// <param name="hdcDst">目的DC</param>
        /// <param name="prcDst">目的矩形</param>
        /// <param name="nControlType">控件类型（MzControlDefs）</param>
        /// <param name="state">控件状态（ControlState）</param>
        /// <returns></returns>
        public static bool DrawControl(IntPtr hdcDst, Rectangle prcDst, uint nControlType, uint ctrState)
        {
            RECT rect = RECT.FromRectangle(prcDst);

            try
            {
                return DrawControl(hdcDst, ref rect, nControlType, ctrState);
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 绘制系统定义的控件外观 
        /// </summary>
        /// <param name="hdcDst">目的DC</param>
        /// <param name="prcDst">目的矩形</param>
        /// <param name="nControlType">控件类型（MzControlDefs）</param>
        /// <param name="state">控件状态（ControlState）</param>
        /// <returns></returns>
        public static bool DrawControl(Graphics g,Rectangle prcDst, uint nControlType, uint ctrState)
        {
            IntPtr hdc = g.GetHdc();

            return DrawControl(hdc, prcDst, nControlType, ctrState);
        }

        /// <summary>
        /// 绘制准备被删除的列表项的背景
        /// </summary>
        /// <param name="hdcDst">目的DC</param>
        /// <param name="prcDst">目的矩形</param>
        /// <returns></returns>
        [DllImport("MZFCWrapper.dll", SetLastError = true, EntryPoint = "MzDrawDeleteItemBgEx")]
        private extern static bool DrawDeleteItemBg(IntPtr hdcDst, ref RECT prcDst);

        /// <summary>
        /// 绘制格子对话框的背景, 可指定是否直角（或圆角）. 
        /// </summary>
        /// <param name="hdcDst">目的DC</param>
        /// <param name="prcDst">目的矩形</param>
        /// <param name="bDrawAngle">是否绘制圆角</param>
        /// <returns></returns>
        [DllImport("MZFCWrapper.dll", SetLastError = true, EntryPoint = "MzDrawGridDlgBGEx")]
        private extern static bool DrawGridDlgBG(IntPtr hdcDst, ref RECT prcDst, bool bDrawAngle);

        /// <summary>
        /// 绘制弹出对话框的背景（带格子）, 底部是圆角的.
        /// </summary>
        /// <param name="hdcDst">目的DC</param>
        /// <param name="prcDst">目的矩形</param>
        /// <returns></returns>
        [DllImport("MZFCWrapper.dll", SetLastError = true, EntryPoint = "MzDrawGridPopupBG_RoundBottomEx")]
        private extern static bool DrawGridPopupBG_RoundBottom(IntPtr hdcDst, ref RECT prcDst);

        /// <summary>
        /// 绘制弹出对话框的背景（带格子）, 顶部是圆角的. 
        /// </summary>
        /// <param name="hdcDst">目的DC</param>
        /// <param name="prcDst">目的矩形</param>
        /// <returns></returns>
        [DllImport("MZFCWrapper.dll", SetLastError = true, EntryPoint = "MzDrawGridPopupBG_RoundTopEx")]
        private extern static bool DrawGridPopupBG_RoundTop(IntPtr hdcDst, ref RECT prcDst);

        /// <summary>
        /// 绘制弹出菜单的背景（带格子）, 宽480，顶部直角.
        /// </summary>
        /// <param name="hdcDst">目的DC</param>
        /// <param name="prcDst">目的矩形</param>
        /// <returns></returns>
        [DllImport("MZFCWrapper.dll", SetLastError = true, EntryPoint = "MzDrawGridPopupMenuBG2_480Ex")]
        private extern static bool DrawGridPopupMenuBG2_480(IntPtr hdcDst, ref RECT prcDst);

        /// <summary>
        /// 绘制弹出菜单的背景（带格子）, 宽720，顶部直角.
        /// </summary>
        /// <param name="hdcDst">目的DC</param>
        /// <param name="prcDst">目的矩形</param>
        /// <returns></returns>
        [DllImport("MZFCWrapper.dll", SetLastError = true, EntryPoint = "MzDrawGridPopupMenuBG2_720Ex")]
        private extern static bool DrawGridPopupMenuBG2_720(IntPtr hdcDst, ref RECT prcDst);

        /// <summary>
        /// 绘制弹出菜单的背景（带格子）, 宽480，顶部圆角.
        /// </summary>
        /// <param name="hdcDst">目的DC</param>
        /// <param name="prcDst">目的矩形</param>
        /// <returns></returns>
        [DllImport("MZFCWrapper.dll", SetLastError = true, EntryPoint = "MzDrawGridPopupMenuBG_480Ex")]
        private extern static bool DrawGridPopupMenuBG_480(IntPtr hdcDst, ref RECT prcDst);

        /// <summary>
        /// 绘制弹出菜单的背景（带格子）, 宽720，顶部圆角. 
        /// </summary>
        /// <param name="hdcDst">目的DC</param>
        /// <param name="prcDst">目的矩形</param>
        /// <returns></returns>
        [DllImport("MZFCWrapper.dll", SetLastError = true, EntryPoint = "MzDrawGridPopupMenuBG_720Ex")]
        private extern static bool DrawGridPopupMenuBG_720(IntPtr hdcDst, ref RECT prcDst);

        /// <summary>
        /// 绘制文本框_带有圆角阴影
        /// </summary>
        /// <param name="hdcDst"></param>
        /// <param name="prcDst"></param>
        /// <returns></returns>
        [DllImport("MZFCWrapper.dll", EntryPoint = "MzDrawInputBox_RoundShadowEx")]
        private static extern bool MzDrawInputBox_RoundShadowEx(IntPtr hdcDst, ref RECT prcDst);

        /// <summary>
        /// 绘制水平滚动条
        /// </summary>
        /// <param name="hdcDst">目的DC</param>
        /// <param name="prcDst">目的矩形</param>
        /// <returns></returns>
        [DllImport("MZFCWrapper.dll", SetLastError = true, EntryPoint = "MzDrawScrollbarHEx")]
        private extern static bool DrawScrollbarH(IntPtr hdcDst, ref RECT prcDst);

        /// <summary>
        /// 绘制竖直滚动条（使用指定的资源，资源ID为 nResID: resource ID of the scrollbar in mzres.dll）
        /// </summary>
        /// <param name="hdcDst">目的DC</param>
        /// <param name="prcDst">目的矩形</param>
        /// <param name="nResID">资源ID(resource ID of the scrollbar in mzres.dll)</param>
        /// <returns></returns>
        [DllImport("MZFCWrapper.dll", SetLastError = true, EntryPoint = "MzDrawScrollbarVEx")]
        private extern static bool DrawScrollbarV(IntPtr hdcDst, ref RECT prcDst, int nResID);

        /// <summary>
        /// 绘制竖直滚动条
        /// </summary>
        /// <param name="hdcDst">目的DC</param>
        /// <param name="prcDst">目的矩形</param>
        /// <returns></returns>
        [DllImport("MZFCWrapper.dll", SetLastError = true, EntryPoint = "MzDrawScrollbarVEx2")]
        private extern static bool DrawScrollbarV(IntPtr hdcDst, ref RECT prcDst);

        /// <summary>
        /// 绘制选中（高亮）状态的列表项的背景 
        /// </summary>
        /// <param name="hdcDst">目的DC</param>
        /// <param name="prcDst">目的矩形</param>
        /// <returns></returns>
        [DllImport("MZFCWrapper.dll", SetLastError = true, EntryPoint = "MzDrawSelectedBgEx")]
        private extern static bool DrawSelectedBg(IntPtr hdcDst, ref RECT prcDst);
        /// <summary>
        /// 绘制选中（高亮）状态的列表项的背景
        /// </summary>
        /// <param name="g">绘图图面</param>
        /// <param name="rectangle">目的矩形</param>
        /// <returns></returns>
        public static bool DrawSelectedBackground(Graphics g,Rectangle rectangle)
        {
            if (g == null || rectangle.IsEmpty)
                return false;

            bool result = false;

            IntPtr hdc = g.GetHdc();

            if (hdc == IntPtr.Zero)
                return result;

            RECT rect = RECT.FromRectangle(rectangle);

            result = DrawSelectedBg(hdc, ref rect);

            g.ReleaseHdc(hdc);

            return result;
        }

        /// <summary>
        /// 绘制选中（高亮）状态的列表项的背景（不绘制上下边缘的线） 
        /// </summary>
        /// <param name="hdcDst">目的DC</param>
        /// <param name="prcDst">目的矩形</param>
        /// <returns></returns>
        [DllImport("MZFCWrapper.dll", SetLastError = true, EntryPoint = "MzDrawSelectedBg_NoLineEx")]
        private extern static bool DrawSelectedBg_NoLine(IntPtr hdcDst, ref RECT prcDst);
        /// <summary>
        /// 绘制选中（高亮）状态的列表项的背景（不绘制上下边缘的线）
        /// </summary>
        /// <param name="g">绘图图面</param>
        /// <param name="rectangle">目的矩形</param>
        /// <returns></returns>
        public static bool DrawSelectedBg_NoLine(Graphics g,Rectangle rectangle)
        {
            if (g == null || rectangle.IsEmpty)
                return false;

            bool result = false;

            IntPtr hdc = g.GetHdc();

            if (hdc == IntPtr.Zero)
                return result;

            RECT rect = RECT.FromRectangle(rectangle);

            result = DrawSelectedBg_NoLine(hdc, ref rect);

            g.ReleaseHdc(hdc);

            return result;
        }

        /// <summary>
        /// 绘制文字，与 Win32 API DrawText()功能是相同的，但lpString可为空而不出异常. 
        /// </summary>
        /// <param name="hDC"></param>
        /// <param name="lpString"></param>
        /// <param name="lpRect"></param>
        /// <param name="uFormat"></param>
        /// <returns></returns>
        [DllImport("MZFCWrapper.dll", EntryPoint = "MzDrawTextEx")]
        public static extern int DrawText(IntPtr hDC, string lpString, ref RECT lpRect, uint uFormat);

        /// <summary>
        /// 截屏。截取prcSrc所指定的屏幕图象, 0 表示截取全屏。
        /// </summary>
        /// <param name="rect"></param>
        /// <returns></returns>
        [DllImport("MZFCWrapper.dll", EntryPoint = "ScreenSnapshotEx", SetLastError = true)]
        private static extern IntPtr ScreenSnapshotAPI(ref RECT prcSrc);

        /// <summary>
        /// 获取客户区域截图
        /// </summary>
        /// <returns></returns>
        [DllImport("MZFCWrapper.dll", EntryPoint = "ClientScreenSnapshot", SetLastError = true)]
        private static extern IntPtr ClientScreenSnapshot();
        /// <summary>
        /// 截屏
        /// </summary>
        /// <param name="prcSrc">截取prcSrc所指定的屏幕图象, 如果prcSrc=Rectangle.Empty则表示截取全屏。</param>
        /// <returns>返回截取的屏幕图像</returns>
        public static Bitmap ScreenSnapshot(Rectangle prcSrc)
        {
            RECT rect = RECT.FromRectangle(prcSrc);

            IntPtr hBitmap = ScreenSnapshotAPI(ref rect);

            if (hBitmap == IntPtr.Zero)
                return null;

            return Bitmap.FromHbitmap(hBitmap);
        }
        /// <summary>
        /// 客户区域截屏
        /// </summary>
        /// <returns>返回当前客户区域的图像</returns>
        public static Bitmap SnapshotClientScreen()
        {
            IntPtr hBitmap = ClientScreenSnapshot();

            if (hBitmap == IntPtr.Zero)
                return null;

            return Bitmap.FromHbitmap(hBitmap);
        }
    }
}
